import pygame,sys,os
from pygame.locals import *
"""hello guys this is my first succesful attempt at making the hero move. I managed even with the background.
     i have a feeling that we'll have problems when we put paul's erl's and my stuff toghether. We will have to make the screeen update without
     losing sprites every time"""

#initialization
pygame.init()
screen=pygame.display.set_mode((800,600))
pygame.display.set_caption("Hero Test")
background=pygame.image.load("space.jpg")
initialPosition=[400,560]
pygame.key.set_repeat(1,1)
missile=pygame.Surface((10,10))
missile.fill((255,0,0))
#______________________________#

#Player Class
class Player(pygame.sprite.Sprite):
    pos=[400,560]
    missileMoving=False
    missileY=pos[1]
##______________________ ##

    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #initialise the parent class

#______________________##

    def loadImage(self,initialPosition):
        self.image=pygame.image.load("hero.jpg").convert()
        self.imagerect=self.image.get_rect() # get the rectangle of the image
        self.imagerect.topleft=initialPosition # set the top left corner to be equal
                                                                #to the pos value (center of the screen)
#____________________________#        

    def display(self):
        screen.blit(background,(0,0)) #display the background
        screen.blit(self.image,self.imagerect) #display the hero
#_____________________________#
    def moveLeft(self):
       self.pos[0]-=5                                                  # move  5 pixels to the left
       self.imagerect.topleft=(self.pos[0],self.pos[1]) #use topleft as reference
       self.updateScreen()                                        #it is usually done by default but it is clearer for me
       print self.pos[0]                                             #see down for this function
       if self.imagerect.topleft==(0,self.pos[1]):       #if the hero reaches the border of the screen
           self.pos[0]=self.pos[0]+5                           #the last movement is ignored because five pixels are added
      
#_______________________________#  same as above function
    def moveRight(self):                                                
       self.pos[0]+=5
       self.imagerect.topright=(self.pos[0]+32,self.pos[1]) #using the top right corner of the image which is situated 32 pixels after the
       self.updateScreen()                                              #to the right of the left corner
       print self.pos[0]
       print self.imagerect.topright[0]

       if self.imagerect.topright[0]>=800:                       # use the corner as a stopper preventing the image from moving
           self.pos[0]=self.pos[0]-5
#_____________________________# function to update screen
    def updateScreen(self): 

       screen.blit(background,(0,0)) #blit the background on the screen
                                                    #it is called after the coordinates of the ship are updated
                                                    # so the background blits on top of the old ship while a new ship appears at the new coordinates
       screen.blit(self.image,self.imagerect) #blits the new ship
       pygame.display.update()
     


#__________________________#
    def drawMissile(self):
        
        self.missileX=self.pos[0]
        pygame.display.update()
        self.missileMoving=True
        

    
        
    def trajectory (self):
        
        screen.blit(background,(0,0))
        screen.blit(self.image,self.imagerect)
        screen.blit(missile,(self.missileX,self.missileY))
        pygame.display.update()
        

    def mainLoop(self):
      while 1:
        if self.missileMoving:
                       self.missileY-=5
                       self.trajectory()
        if self.missileY==0:
            self.missileMoving=False
            self.missileY=self.pos[1]

        for event in pygame.event.get():
            if event.type== QUIT:
                exit()
            if event.type== KEYDOWN:
                if event.key==K_LEFT:
                    self.moveLeft()
                    
                if event.key==K_RIGHT:
                   self.moveRight()
                if event.key==K_SPACE:
                    self.drawMissile()
                    
                    
                    

#_______________# main
ship=Player()
ship.loadImage(ship.pos)
ship.display()
pygame.display.update()
ship.mainLoop()







        
        
